package com.int_tech.mession.service;

import com.int_tech.mession.dao.MessionDao;
import com.int_tech.mession.entity.Mession;
import com.int_tech.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

/**
 * Created by Administrator on 2017/9/20.
 */
@Service
public class MessionService {

    @Autowired
    private MessionDao messionDao;

    @Value("${size}")
    private int size;

    /**
    * TODO 查询所有任务
    * @return Result(List<Mession>)
    * @author dmy
    */
    public Result findAllMession(){
        try {
            List<Mession> list = messionDao.findAll();
            return Result.ok(list);
        } catch (Exception e) {
        	// TODO: handle exception
        	e.printStackTrace();
            return Result.build(500,"异常");
        }
    }

    /**
    * TODO 分页查询任务
    * @param currentPage 当前页
    * @return Result(List<Mession>)
    * @author dmy
    */
    public Result findMessionByPage(int currentPage){
        try {
            PageRequest pageRequest = new PageRequest(currentPage-1,size);
            Page<Mession> page = messionDao.findAll(pageRequest);
            List<Mession> list = new ArrayList<Mession>();
            page.forEach(new Consumer<Mession>() {
                @Override
                public void accept(Mession mession) {
                    list.add(mession);
                }
            });
            return Result.ok(list);
        } catch (Exception e) {
        	// TODO: handle exception
        	e.printStackTrace();
            return Result.build(500,"异常");
        }
    }

    /**
    * TODO 根据任务单号查询任务信息
    * @param messionNumber
    * @return Result(Mession)
    * @author dmy
    */
    public Result findMessionByMessionNumber(String messionNumber){
        try {
            Mession mession = messionDao.findMessionByMessionNumber(messionNumber);
            return Result.ok(mession);
        } catch (Exception e) {
        	// TODO: handle exception
        	e.printStackTrace();
            return Result.build(500,"异常");
        }
    }

    public List<Mession> findByCarNumber(String carNumber) {
        return messionDao.findByCarNumber(carNumber);
    }
}
